/**
 * @Description:
 * @Author 卞鹏飞 <228443632@qq.com>
 * @create 07/08/24 3:58 PM
 */

// types
import type {
  Router,
  RouteRecord,
  RouteLocationRaw,
  RouteLocationNormalizedLoaded,
  NavigationFailure
} from 'vue-router'
import type { IAppStateMenuDataItem } from '@root/types/app-store.ts'
import type { TabItemUnion } from '@root/types/layout-store.ts'

export type TRouteRecord = RouteRecord & { __isDynamic: boolean }

export type TRouteRecordList = TRouteRecord[]

export type TRouteLocationRaw = RouteLocationRaw & {
  refresh?: boolean
}

export interface ICustomRouter extends Router {
  push(to: TRouteLocationRaw): Promise<NavigationFailure | void | undefined>

  replace(to: TRouteLocationRaw): Promise<NavigationFailure | void | undefined>

  __vars: {
    /**
     * 路由刷新
     * @param {TRouteLocationRaw} route
     */
    refreshRoute: (route: TRouteLocationRaw) => void
  }

  /**
   * 更新tab 信息
   * @param {Exclude<TabItemUnion, string>} tab
   */
  updateTab: (tab: Exclude<TabItemUnion, string>) => void

  /**
   * 关闭tab
   * @param {Exclude<TabItemUnion, string>} tab
   * @param {{autoJump: boolean}} options
   * @return {Promise<void>}
   */
  closeTab: (tab: Exclude<TabItemUnion, string>, options?: { autoJump: boolean }) => Promise<void>

  /**
   * 刷新tsb
   * @param {({key?: string, fullPath?: string} & string & {refresh?: boolean}) | ({key?: string, fullPath?: string} & RouteQueryAndHash & LocationAsPath & RouteLocationOptions & {refresh?: boolean}) | ({key?: string, fullPath?: string} & RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions & {refresh?: boolean}) | string} tab
   */
  refreshTab: (tab: ({ key?: string; fullPath?: string } & TRouteLocationRaw) | string) => void

  /**
   * 获取路由地址 路径
   * @param {RouteLocationRaw} to
   * @param {RouteLocationNormalizedLoaded} currentLocation
   * @return {string}
   */
  resolveRouteLocationHref: (to: RouteLocationRaw, currentLocation?: RouteLocationNormalizedLoaded) => string

  /**
   * 替换路由地址，且不生成路径
   * @param {RouteLocationRaw} to
   * @param {RouteLocationNormalizedLoaded} currentLocation
   */
  replaceRouteLocationHref: (to: RouteLocationRaw, currentLocation?: RouteLocationNormalizedLoaded) => void

  /**
   * 设置异步路由
   * @param {TRouteRecordList} routes
   */
  __SET_CACHE_ASYNC_ROUTES: (routes: TRouteRecordList) => void

  /**
   * 路由关闭之前回调函数
   * @param {Function & {key: string}} cb
   */
  onBeforeCloseRoute: (cb: Function & { key: string }) => void

  /**
   * 是否有权限跳转
   * @param {string} path
   * @return {{hasPermission: boolean, targetMenu: IAppStateMenuDataItem}}
   */
  __AUTH_ROUTE_NEXT_TO__: (path: string) => { hasPermission: boolean; targetMenu: IAppStateMenuDataItem }
}

export type TRouter = ICustomRouter
